<?php

namespace my;

require_once 'alipay/aop/AopClient.php';
require_once 'alipay/aop/request/AlipayTradeRefundRequest.php';

use think\Log;

/**
 * 阿里支付
 */
class AliPay2
{
    protected $config;
    public $err_msg = '';

    public function __construct($conf = [])
    {
        $config = [
            'gatewayUrl'         => 'https://openapi.alipay.com/gateway.do',
            'appId'              => '',//应用ID
            'signType'           => 'RSA2',
            'notifyUrl'          => '',//回调地址
            'rsaPrivateKey'      => '',//应用私钥
            'alipayrsaPublicKey' => '',//支付宝公钥
        ];
        $this->config = array_merge($config, $conf);
    }

    public function unifiedOrder_refund($out_trade_no, $total_amount, $subject = '')
    {
        $aop = new \AopClient;
        $aop->gatewayUrl         = $this->config['gatewayUrl'];
        $aop->appId              = $this->config['appId'];
        $aop->signType           = $this->config['signType'];
        $aop->rsaPrivateKey      = $this->config['rsaPrivateKey'];
        $aop->alipayrsaPublicKey = $this->config['alipayrsaPublicKey'];
        $aop->apiVersion         = '1.0';
		$object = [
            'out_trade_no' => $out_trade_no,
            'refund_amount' => $total_amount,
            'out_request_no' => $out_trade_no,
        ];
		$json = json_encode($object);
		$request = new \AlipayTradeRefundRequest();
		$request->setBizContent($json);
		$result = $aop->execute ($request); 
        
        try {
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            if(!empty($resultCode)&&$resultCode == 10000){
                return true;
            } else {
                return false;
            }
        } catch (\Throwable $th) {
            //throw $th;
            return $th;
        }

		
    }

}